package com.icangya.DAO.impl;


import com.icangya.DAO.IAdvertRecordDAO;
import com.icangya.Utils.SQLUtilt;
import com.icangya.domain.AdvertRecord;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.icangya.Value.FootBall.ADVERT_ROW;

@Repository
public class AdvertRecordDAOImpl extends BaseDAO<AdvertRecord> implements IAdvertRecordDAO {


    Logger logger = LoggerFactory.getLogger(this.getClass());
    @Override
    public List getAdvertRecord(String openId) {
        String hql="FROM AdvertRecord ar where ar.openId=?";
       List<AdvertRecord> list= (List<AdvertRecord>) getHibernateTemplate().find(hql,openId);
        if (list != null && list.size() > 0)
            return list;
        return null;
    }

    @Override
    public List getAdvertRecordShame() {

        Session session=  getHibernateTemplate().getSessionFactory().getCurrentSession();
        String hql="FROM AdvertRecord ad where ad.status=0 ORDER BY ad.advertMoney desc";
        Query query=session.createQuery(hql);
        query.setMaxResults(1000);
        List list=query.list();
        logger.info("advert1000:=================list.size==============================="+list.size());
        if(list !=null)
            return  list;
        return null;
    }

    @Override
    public List getAdvertAll() {

        String hql=" FROM AdvertRecord ad where ad.status=0 ORDER BY ad.advertMoney desc";

        return  getHibernateTemplate().find(hql);

    }

    @Override
    public List getAdvertAll(int startPage,int advertRow) {

        String sel="";
        String hql="";
        if(advertRow==ADVERT_ROW){
            sel=" FROM AdvertRecord ad where ad.status=0 ORDER BY ad.advertMoney desc";
            hql=" FROM AdvertRecord ar where ar.status=0";
        }else{
          sel=" FROM AdvertRecord ad  ORDER BY ad.advertMoney desc";
          hql=" FROM AdvertRecord ar ";
        }
        String count="SELECT count(ar) "+hql;
        Long total= (Long) getHibernateTemplate().find(count).get(0);

        if (total <= 0)
            return null;
        return SQLUtilt.getIstance().doPage(getHibernateTemplate(), sel, total, startPage, advertRow);

    }

    @Override
    public List getAdvertNotClick(String openId, int startPage) {

//        select * from advert_record  where id not in(
//                select advertId from advert_click where openid='oyGuGwjIZ6vOhOJ6z9Augno3TO90')

//      String hq="  select   SQL_CALC_FOUND_ROWS ar from advert_record ar where ar.id not in( select ac.advertId from advert_click  ac where ac.openid= ? )";
//     String count=" select  FOUND_ROWS() as rowcount";

        String hql="select ar from AdvertRecord ar where ar.id not  in ( select ac.advertId from AdvertClick  ac where ac.openId =? ) and ar.status=0 ORDER BY ar.advertMoney desc";

        String sel="select count(ar)  from AdvertRecord ar where ar.id  not in( select ac.advertId from AdvertClick  ac where ac.openId= '"+openId+" ') and ar.status=0";     //from advert_record ar where ar.id not in( select ac.advertId from advert_click  ac where ac.openid= ? )";
        Long total= (Long) getHibernateTemplate().find(sel).get(0);
        System.out.println("advertCount:"+total);




        return SQLUtilt.getIstance().doPage(getHibernateTemplate(), hql, total, startPage, ADVERT_ROW,openId);




    }

    @Override
    public AdvertRecord getAdvertRecord(Long id) {

        String hql=" FROM AdvertRecord ar where ar.id=? ";
        List<AdvertRecord> list= (List<AdvertRecord>)getHibernateTemplate().find(hql,id);
        if (list != null && list.size() > 0)
            return list.get(0);
        return null;

    }

    @Override
    public int updateAdvertMoney(double money, Long id) {
        int result = getHibernateTemplate().execute(new HibernateCallback<Integer>() {

            @Override
            public Integer doInHibernate(Session session) throws HibernateException {
                String hql="update AdvertRecord  ar set ar.advertMoney=ar.advertMoney+?,ar.modifiedTime=? where ar.id=?";
                Query query=session.createQuery(hql);
                query.setParameter(0,money);
                query.setParameter(1,new Timestamp(new Date().getTime()));
                query.setParameter(2,id);
               int ret= query.executeUpdate();
                logger.warn("修改广告金额为 == " + money + "|修改用户Id是 == " + id +"|结果 == " + ret);
                return ret;
            }
        });

        return result;
    }

    @Override
    public int updateAdvertByStatus(Long id) {


       int result= getHibernateTemplate().execute(new HibernateCallback<Integer>() {
           @Override
           public Integer doInHibernate(Session session) throws HibernateException {


               String hql="update AdvertRecord  ar set ar.status=1 where ar.id=?";
               Query query=session.createQuery(hql);
               query.setParameter(0,id);
               int ret= query.executeUpdate();
               logger.warn("|修改广告Id是 == " + id +"|结果 == " + ret);
               return ret;
           }
       });
        return result;
    }

    @Override
    public int updateAdvertTopMoney(long id, double money) {

        int result=getHibernateTemplate().execute(new HibernateCallback<Integer>() {
            @Override
            public Integer doInHibernate(Session session) throws HibernateException {

                String hql="update AdvertRecord  ar set ar.status=0,ar.advertTopMoney=ar.advertTopMoney+? where ar.id=?";
                Query query=session.createQuery(hql);
                query.setParameter(0,money);
                query.setParameter(1,id);
                int ret= query.executeUpdate();
                logger.warn("|修改广告Id是 == " + id +"|结果 == " + ret+"修改的金额为："+money);
                return ret;

            }
        });
        return result;
    }


    @Override
    public int updateAdvertShampageview(long id, long ramd) {

        int result = getHibernateTemplate().execute(new HibernateCallback<Integer>() {
            @Override
            public Integer doInHibernate(Session session) throws HibernateException {

                String hql = "update AdvertRecord  ar set ar.shamPageView = ? where ar.id=?";
                Query query = session.createQuery(hql);
                query.setParameter(0, ramd);
                query.setParameter(1, id);
                int ret = query.executeUpdate();
//                logger.warn("增加广告ID为：" + id + " 的虚假浏览量为 " + ramd);
                return ret;

            }
        });
        return result;
    }


}
